//
//  LWRefreshView.swift
//  刷新控件
//
//  Created by kobe on 2016/12/29.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit

//刷新视图 - 负责相关的UI显示和动画
class LWRefreshView: UIView {
    
    //刷新状态
    /*
        iOS 系统中 UIView 封装的旋转动画默认顺时针旋转  就近原则
     */
    var refreshState:LWRefreshState = .Normal {
        didSet {
            switch refreshState {
            case .Normal:
                
                // 恢复状态
                tipIcon?.isHidden = false
                indicator?.stopAnimating()
                
                tipLabel?.text = "继续使劲拉..."
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon?.transform = CGAffineTransform.identity
                })
            case .Pulling:
                tipLabel?.text = "放手就刷新..."
                UIView.animate(withDuration: 0.25, animations: { 
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI - 0.001))
                })
                
            case .WillRefresh:
                tipLabel?.text = "正在刷新中..."
                
                // 隐藏提示图标
                tipIcon?.isHidden = true
                // 显示菊花
                indicator?.startAnimating()
                
            }
        }
    }
    
    //父视图高度
    var parentViewHeight: CGFloat = 0

    /// 提示图标
    @IBOutlet weak var tipIcon: UIImageView?

    /// 提示标签
    @IBOutlet weak var tipLabel: UILabel?

    /// 指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    
    
    class func refreshView() -> LWRefreshView {
        let nib = UINib(nibName: "LWMeituanRefresh", bundle: nil)
        return nib.instantiate(withOwner: nil, options: nil)[0] as! LWRefreshView
    }
    
    
    
}
